﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Web.Action;
using PowerCreator.LMS.Action.Entity;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_RoleManager_SetAuthorityAction : PowerCreator.LMS.Web.Base.BasePage
{
    private string _SysColumnID = "1";        //该界面默认加载的栏目类型为1

    PowerCreator.LMS.Web.Action.AuthorityAction opAction = new PowerCreator.LMS.Web.Action.AuthorityAction();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["authorityID"] != null)
            {
                string authorityID = Request.QueryString["authorityID"].ToString();
                ViewState["authorityID"] = authorityID;
            }
            BindRpt();
            InitPage();
        }
    }

    protected override void InitPage()
    {
        chkAll.Text = GetLanageValueByKey("SetBaseAction_rdbtn_YesAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkNoAll.Text = GetLanageValueByKey("SetBaseAction_rdbtn_NoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkYes.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSelect.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_ShowUser.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o5", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_RoleUser.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o4", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }
    /// <summary>
    /// 绑定权限信息
    /// </summary>
    private void BindRpt()
    {
        string[] authorityType = new string[5];
        authorityType[0] = "区域设置";
        authorityType[1] = "学校设置";
        authorityType[2] = "班级设置";
        authorityType[3] = "专业设置";
        authorityType[4] = "课程设置";
        PowerCreator.LMS.Web.Action.AuthorityAction opAction = new PowerCreator.LMS.Web.Action.AuthorityAction();
        //string searchc1 = string.Format("ActioncoluType={0} and ActioncoluMenuIsVisible = 1", _SysColumnID);
        //DataTable dt = opAction.QueryColumnAction(searchc1, "", opAction.QueryColumnActionCount(searchc1), 1);
        //dt.Columns.Add("ActioncoluVariableLG");
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    dt.Rows[i]["ActioncoluVariableLG"] = GetLanageValueByKey(dt.Rows[i]["ActioncoluVariable"].ToString(), PowerCreator.LMS.Language.LanagetItemType.MenuItemColumn);
        //}
        //rptColumnType.DataSource = dt;
        //rptColumnType.DataBind();
        DataTable dt=new DataTable();
        dt.Columns.Add("authorityTypeID");
        dt.Columns.Add("authorityTypeName");
        for (int i = 0; i < authorityType.Length;i++ )
        {
            dt.Rows.Add(i);
            dt.Rows[i]["authorityTypeID"] = i;
            dt.Rows[i]["authorityTypeName"] = authorityType[i];
        }
        rptColumnType.DataSource = dt;
        rptColumnType.DataBind();
        string search = string.Format("FkAuthorityID={0}", ViewState["authorityID"]);
        int pageSize = opAction.QueryAuthorityListCount(search);
        DataTable dtAuthorityAction = opAction.QueryAuthorityList(search, pageSize, 1);
        Hashtable hs = new Hashtable();
        ArrayList list = new ArrayList();

        for (int i = 0; i < dtAuthorityAction.Rows.Count; i++)
        {
            hs.Add(dtAuthorityAction.Rows[i]["authorityListID"].ToString(), "1");
        }

        for (int i = 0; i < authorityType.Length; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            Label lblColumnID = (Label)rptColumnType.Items[i].FindControl("lblColumnID");
            DataTable dt1 = opAction.QueryActionByColumnID(int.Parse(lblColumnID.Text));
            //for (int k = 0; k < dt1.Rows.Count; k++)
            //{
            //    string an = GetLanageValueByKey(dt1.Rows[k]["ActionVariable"].ToString(), PowerCreator.LMS.Language.LanagetItemType.MenuItem);
            //    if (!string.IsNullOrEmpty(an))
            //        dt1.Rows[k]["ActionName"];//= an;
            //    if (dt1.Rows[k]["IsShow"].ToString() == "0")
            //    {
            //        dt1.Rows[k].Delete();
            //    }
            //}
            chkList.DataSource = dt1;
            chkList.DataTextField = "authorityName";
            chkList.DataValueField = "authorityID";
            chkList.DataBind();
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                if (hs.ContainsKey(chkList.Items[j].Value))
                    chkList.Items[j].Selected = true;
            }
        }
    }

    //全选
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                chkList.Items[j].Selected = true;
            }
        }
    }
    //取消
    protected void chkNoAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                chkList.Items[j].Selected = false;
            }
        }
    }
    //反选
    protected void chkYes_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                if (chkList.Items[j].Selected)
                    chkList.Items[j].Selected = false;
                else
                    chkList.Items[j].Selected = true;
            }
        }
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptColumnType.Items.Count; i++)
        {
            CheckBoxList chkList = (CheckBoxList)rptColumnType.Items[i].FindControl("chklstAction");
            for (int j = 0; j < chkList.Items.Count; j++)
            {
                string actionid = chkList.Items[j].Value;
                string search = string.Format("authorityType={0} and FkOID={1}", ViewState["authorityID"], actionid);
                DataTable dt = opAction.QueryAuthorityList(search, 1, 1);

                if (chkList.Items[j].Selected)
                {
                    if (dt.Rows.Count == 0)
                    {
                        VOTauthorityList vo = new VOTauthorityList();
                        vo.FkOID = int.Parse(actionid);
                        vo.FKCreatorId = GetLoginEntity.UserID;
                        vo.FkAuthorityID = int.Parse(ViewState["authorityID"].ToString());
                        vo.CreateTime = DateTime.Now;
                        vo.AuthorityType = i;
                        opAction.AddAuthorityListAction(vo);
                    }
                }
                else
                {
                    if (dt.Rows.Count == 1)
                        opAction.DelAuthorityListAction(int.Parse(dt.Rows[0]["authorityListID"].ToString()));
                }
            }
            lbl_Save_Msg.Text = GetLanageValueByKey("SetBaseAction_msg_save1", PowerCreator.LMS.Language.LanagetItemType.Page);
        }

    }
    protected void btn_ShowUser_Click(object sender, EventArgs e)
    {
        //用户查看
        string url = string.Format("AuthorityUser.aspx?authorityID={0}", ViewState["authorityID"]);
        Script.RedirectURL(url);
    }
    protected void btn_RoleUser_Click(object sender, EventArgs e)
    {
        //用户授权
        string url = string.Format("authoritySelectUser.aspx?authorityID={0}", ViewState["authorityID"]);
        Script.RedirectURL(url);
    }
}
